<?php
/**
 * Miffos framework
 * ----------------------------------------------
 *
 * Copyright (c) 2008, 2009 Martin Sadový (http://sodae.cz)
 *
 * This source file is subject to the "Miffos license" that is bundled
 * with this package in the file license.txt.
 *
 * @copyright  Copyright (c) 2008, 2009 Martin Sadový
 * @license    license.txt
 * @link       http://miffos.sodae.cz
 * @package    miffos
 * @version    $Id$
 */

    
    class Config {

        /**
         * Config Data
         * @var array
         */
        private $data = array();


        public function __construct()
        {
            $this->multiSet((array)$this->loadYamlFile());
        }



        /**
         * Load Yaml file
         * @param string $file
         * @return array
         */
        public function loadYamlFile($file="/config.yaml.php")
        {
            if(file_exists(Application::$appDir.$file)){
                require_once dirname(__FILE__).'/Config/spyc.php';
                $config = Spyc::YAMLLoad(Application::$appDir.$file);
            }

            if(is_dir(Cache::$dir)){
                $cache = new Cache('Miffos\Application\Config');
                if(($cache['data']['debug'] == 0) AND is_array($cache['data'])){
                    return $cache['data'];
                }
                $cache['data'] = $config;
            }
            
            return $config;
        }



        /** Over loading **/
        public function __get($name)
        {
            return $this->data[$name];
        }

        public function __unset($name)
        {
            unset($this->data[$name]);
        }

        public function __set($name,$value)
        {
            $this->data[$name] = $value;
        }

        public function __isset($name)
        {
            return isset($this->data[$name]);
        }

        public function multiSet(array $array){

            foreach($array as $key=>$value){
                $this->__set($key,$value);
            }

        }
    }
?>